/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.service.impl;

import java.net.Socket;
import java.util.List;

import org.apache.axis.MessageContext;
import org.apache.axis.components.net.BooleanHolder;
import org.apache.axis.transport.http.HTTPSender;
import org.apache.axis.transport.http.SocketHolder;

import com.cqcis.uip.base.framework.common.Task;
import com.cqcis.uip.base.framework.core.RegisteredService;
import com.cqcis.uip.base.framework.handlers.common.AbstractWebServiceCallerHandler;
import com.cqcis.uip.base.framework.utils.WSClientThreadLocalTask;

/**
 * @author huangmy
 * @date   2008-11-3
 */
public class HTTPSenderAdaptor extends HTTPSender {
	private static final long serialVersionUID = 1L;
	private RegisteredService currentRS;
		
	public HTTPSenderAdaptor(RegisteredService currentRS) {
		super();
		this.currentRS = currentRS;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void getSocket(SocketHolder sockHolder,
			MessageContext msgContext, String protocol, String host, int port,
			int timeout, StringBuffer otherHeaders, BooleanHolder useFullURL)
			throws Exception {
		Task task = WSClientThreadLocalTask.getTask(this.currentRS);		
		long timeoutLong = (Long)task.getAttribute(AbstractWebServiceCallerHandler.WSCLIENT_SOCKET_TIMEOUT);
		timeout = (int)timeoutLong;
		
		if (timeout < 0) {
			timeout = 60 * 1000;
		}
		
		super.getSocket(sockHolder, msgContext, protocol, host, port, timeout,
				otherHeaders, useFullURL);
		
		Socket socket = sockHolder.getSocket();
		if (socket != null) {			
			IntfLogByteBufferAdaptor record = new IntfLogByteBufferAdaptor(task);
			
			List<IntfLogByteBufferAdaptor> records = (List<IntfLogByteBufferAdaptor>)task.getAttribute(AbstractWebServiceCallerHandler.WSCLIENT_INTF_LOG);
			records.add(record);
			
			Socket sock = new SocketRecordBufferAdator(socket, record.getSendBuf(), record.getRecvBuf(), record.getIntfLog());
			sockHolder.setSocket(sock);
			task.setAttribute(AbstractWebServiceCallerHandler.WSCLIENT_BINDTASK_SOCKET, sock);
		}
	}
}
